package com.hermes.action.login;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.user.UserDO;
import com.hermes.biz.common.util.login.CorpLoginUtil;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.ParameterParserUtil;

public class CorpSinaLoginProcessAction  extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(CorpSinaLoginProcessAction.class);
	private static CorpLoginUtil clu = new CorpLoginUtil();
	private UserService userService;
	
	@Override
	public String execute() throws IOException {
		
		String codeRes = ParameterParserUtil.getParameter("code");

		if(StringUtil.isNotBlank(codeRes)){
			String getATRes = executeSendPost(clu.sinaGetAccessTokenUrl,"&code="+codeRes);
			JSONObject jo = JSONObject.fromObject(getATRes);
			String atokenStr = jo.getString("access_token");
			String uidStr = jo.getString("uid");
			String getUserParamStr = "access_token="+atokenStr+"&uid="+uidStr;
			String userInfoJsonStr = executeSend(clu.getSinaUserInfoUrl,getUserParamStr);
			JSONObject userInfo = JSONObject.fromObject(userInfoJsonStr);
			logger.error(userInfoJsonStr);
			String userName = userInfo.getString("name");
			String userHeadPic = userInfo.getString("profile_image_url");
			String userIdStr = userInfo.getString("id");
			session.put("userName", userName);

			long randomUserId = System.currentTimeMillis();
			if(randomUserId<=0){
				randomUserId=0-randomUserId;
			}
			UserDO userDO = new UserDO();
			userDO.setAccessTokenSina(atokenStr);
			userDO.setOpenIdSina(userIdStr);
			userDO.setUserId(randomUserId);
			userDO.setUserName(userName);
			userDO.setHeadPic(userHeadPic);
			userDO.setUserPassword("888888");
			
			userService.registerNewUserAccount(userDO);
			
			session.put("userId", randomUserId);
			
	        HttpServletResponse response = ServletActionContext.getResponse();
			response.sendRedirect("/index.htm");
		}
		else{
			//进这里是傻逼
			logger.error("nnd");
		}
		return null;
	}
	
	public  String executeSend(String url ,String param){
		try{
			String sendURL = url+param;
			HttpClient hc = new DefaultHttpClient();
			HttpGet httpget= new HttpGet(sendURL);  
			HttpResponse response =hc.execute(httpget);
	        HttpEntity entity = response.getEntity();  

	        if (entity != null) {  
		        return EntityUtils.toString(entity);
	        } 
		}
		catch(Exception e){
			logger.error(e);
		}
		return "";

	}

	public  String executeSendPost(String url ,String param){
		try{
			
			String sendURL = url+param;
			HttpClient hc = new DefaultHttpClient();
			HttpPost httpget= new HttpPost(sendURL);  
			HttpResponse response =hc.execute(httpget);
	        HttpEntity entity = response.getEntity();  

	        if (entity != null) {  
	        	 return EntityUtils.toString(entity);
	        } 
		}
		catch(Exception e){
			logger.error(e);
		}
		return "";

	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
